/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Interface;

import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import remotechessplayer.RemoteChessPlayer;

/**
 *
 * @author agnieszka
 */
public class Action {                                                           


    public InterfaceJPanel jPanel;
    public RemoteChessPlayer player = new RemoteChessPlayer();

    public JFrame  window_settings1 = new JFrame("On-line Player 1 Settings");
    public JFrame window_settings2 = new JFrame("On-line Player 2 Settings");
    
    public boolean is_white = false, load=false, zerogame=true;
    public boolean without_board=false, long_white=true, long_black=true,short_white=true, short_black=true;
    public boolean settings1_is_on, settings2_is_on;
    public boolean [] moveboard = new boolean[4];

    public Point too = new Point(0,0);
    public Point fromm = new Point (0,0);
    public Point move_before_from=new Point(0,0), move_before_to=new Point(0,0);
   
    public String str = new String("\n");
    public char [][] toloadedboard = new char[8][8], loadedboard = new char[8][8];
    public char [][] board = new char[8][8];
    public char [] color=new char[2];
    
    
    public int i=1, last_move;

    
            Action(InterfaceJPanel jPan) {
               jPanel =jPan;
            }


         public boolean [] make_moveboard() {     //info about possibility of casteling

                if(fromm.x==4 && fromm.y==0 && board[4][0]=='k' && (short_black==true || long_black==true))
                    {
                         long_black=false;
                         short_black=false;
                    }
                       else{

                                if(fromm.x==0 && fromm.y==0 && board[0][0]=='r' && long_black==true) long_black=false;
                                if(fromm.x==7 && fromm.y==0 && board[7][0]=='r' && short_black==true) short_black=false;
                        }

                if(fromm.x==4 && fromm.y==7 && board[4][7]=='K' && (short_white==true || long_white==true))
                    {
                         long_white=false;
                         short_white=false;
                    }
                        else{
                                if(fromm.x==0 && fromm.y==7 && board[0][7]=='R' && long_white==true) long_white=false;
                                if(fromm.x==7 && fromm.y==7 && board[7][7]=='R' && short_white==true) short_white=false;

                        }


                    moveboard[0]=long_white;
                    moveboard[1]=short_white;
                    moveboard[2]=long_black;
                    moveboard[3]=short_black;


                    return moveboard;
          }


     public char [][] makeboard() {

                                                    //make the boardgame- small letter is black figure
                                                    //capital letter - white figure
            moveboard[0]=long_white;
            moveboard[1]=short_white;
            moveboard[2]=long_black;
            moveboard[3]=short_black;

            board[0][0] = board[7][0]= 'r';         // rook
            board[1][0] = board[6][0]= 'n';          //knight
            board[2][0] = board[5][0]= 'b';         //bishop
            board[3][0] = 'q';                      //queen
            board[4][0] = 'k';                      //king

            for(int c = 0;c<=7; c++) board[c][1]='p';   //pawn

            //white figures
            board[0][7] = board[7][7]= 'R';
            board[1][7] = board[6][7]= 'N';
            board[2][7] = board[5][7]= 'B';
            board[3][7] = 'Q';
            board[4][7] = 'K';

            for(int c = 0;c<=7; c++) board[c][6]='P';

            for(int c = 0;c<=7; c++)
                for(int r = 2;r<=5; r++)board[c][r]='0';

            draw(board);

            return board;
        }

    public void update_board(int which_move, Point from, Point to) {


              too=to;
              fromm=from;


              to.x=too.x;                                                               //change chessboard's cordinates for neural and gameboard cord.
              to.y=Math.abs(too.y-7);
              from.x=fromm.x;
              from.y=Math.abs(fromm.y-7);

              jPanel.players.move_before_2[0]=(Point) from.clone();
              jPanel.players.move_before_2[1]=(Point) to.clone();

              zerogame=false;

              jPanel.players.tab_castel = make_moveboard();

              if(jPanel.players.gameboard[to.x][to.y]=='K'||jPanel.players.gameboard[to.x][to.y]=='k') jPanel.players.end=true;


              jPanel.players.gameboard[to.x][to.y]=jPanel.players.gameboard[from.x][from.y];

              jPanel.players.gameboard[from.x][from.y]='0';

              if(jPanel.players.gameboard[to.x][to.y]=='p' && jPanel.what_figure!=1000){

                    switch(jPanel.what_figure){

                            case 1: jPanel.players.gameboard[to.x][to.y]='r';
                                     break;

                            case 2: jPanel.players.gameboard[to.x][to.y]='n';
                                     break;

                            case 3: jPanel.players.gameboard[to.x][to.y]='b';
                                     break;

                            case 4: jPanel.players.gameboard[to.x][to.y]='q';
                                     break;

                            default: break;
                    }

                    jPanel.what_figure=1000;
              }

              if(jPanel.players.gameboard[to.x][to.y]=='P' && jPanel.what_figure!=1000){

                    switch(jPanel.what_figure){

                            case 1: jPanel.players.gameboard[to.x][to.y]='R';
                                     break;

                            case 2: jPanel.players.gameboard[to.x][to.y]='N';
                                     break;

                            case 3: jPanel.players.gameboard[to.x][to.y]='B';
                                     break;

                            case 4: jPanel.players.gameboard[to.x][to.y]='Q';
                                     break;

                            default: break;
                    }

                    jPanel.what_figure=1000;
              }

              board=jPanel.players.gameboard;

              too=to;
              fromm=from;
              last_move=which_move;


       }

    public void update_board_by_eng(int which_move, Point from, Point to) {

              too=to;
              fromm=from;

              jPanel.players.move_before_2[0]=(Point) fromm.clone();
              jPanel.players.move_before_2[1]=(Point) too.clone();

              zerogame=false;
              jPanel.players.tab_castel=make_moveboard();
              jPanel.players.gameboard[to.x][to.y]=jPanel.players.gameboard[from.x][from.y];
              jPanel.players.gameboard[from.x][from.y]='0';

              board=jPanel.players.gameboard;


              last_move=which_move;


       }

   public void draw(char [][] board) {                                          //draw in output gameboard

              int poz=0;

              for(int row=0;row<board.length;row++){
                        for(int c=0;c<board[0].length;c++){

                            System.out.print(" " + board[c][row] );
                            }

                       System.out.print("\n");

        }}

   public void configure1() {

              if(window_settings1.isShowing()==false) settings1_is_on=false;

              if(jPanel.bRb1_ous && jPanel.bBsettings1 && settings1_is_on==false){

                    settings1_is_on=true;
                    window_settings1.setBounds(700, 250, 258,460);
                    window_settings1.setResizable(false);
                    window_settings1.add(jPanel.onlineplayer1);
                    window_settings1.setVisible(true);

         }

              if(jPanel.bRb1_ous && jPanel.bBsettings1 && settings1_is_on) window_settings1.toFront();

         }


   public void configure2() {

              if(window_settings2.isShowing()==false) settings2_is_on=false;

              if(jPanel.bRb2_ous && jPanel.bBsettings2 && settings2_is_on==false){

                    settings2_is_on=true;
                    window_settings2.setBounds(990, 250, 258,460);
                    window_settings2.setResizable(false);
                    window_settings2.add(jPanel.onlineplayer2);
                    window_settings2.setVisible(true);

                 }

              if(jPanel.bRb2_ous && jPanel.bBsettings2 && settings2_is_on) window_settings2.toFront();


        }

    public void savegame() {

            try{

               FileOutputStream gametowrite = new FileOutputStream("game.txt");
               FileOutputStream  colortowrite = new FileOutputStream("colorOFgame.txt");

                  for(int row=0;row<board.length;row++){
                    for(int c=0;c<board[0].length;c++){

                        gametowrite.write(board[c][row]);
                    }

        }

               char [] color=new char[2];
               color[0]='1';
               color[1]='0';
               if(last_move%2==0) colortowrite.write(color[0]);    //now should be white (after load)
                   else colortowrite.write(color[1]);                   //now should be black

            } catch(IOException ex){

               System.out.println("Error: problem with write "+ex);
            }
            }

    public void loadgame() {

            try{

               FileReader fis = new FileReader("game.txt");
               BufferedReader br = new BufferedReader(fis);
               int bajt = 0;
               int c,r;
               c=r=0;
                   while((bajt = fis.read()) != -1){

                        toloadedboard[c][r]=(char)bajt;
                        loadedboard[c][Math.abs(r-7)]=(char)bajt;
                        c++;
                        if(c>7){
                            c=0;
                            r++;
                        }
                        if(r>7)break;

                   }

                 draw(toloadedboard);

               MainInterface.exchange.setInstantMove(loadedboard);                  //set figure on the chessboard

               FileReader col = new FileReader("colorOFgame.txt");
               BufferedReader bufcol = new BufferedReader(col);

               char coll=(char)col.read();
               System.out.println("COLL"+coll);
               if(coll=='0')    is_white=false;
               if(coll=='1') is_white=true;

               zerogame=false;
               load=true;


            } catch (IOException ex){

                load=false;
                JOptionPane.showMessageDialog(MainInterface.okno, "No saved game", "WARNING",0);
                System.out.println("Błąd przy odczycie danych: "+ex);
                jPanel.bBload=false;

            }


        }

    }




